/**
 * Created by Administrator on 2017/4/19.
 */
$(function(){
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
   var request = new Object();
    request = GetRequest();
    var aid;
    aid = request["aid"];
    //展示用户信息
    var name,signText,age,height,weight,constellation,linkType,purpose,headImg,photoWall,acceptIntimateBehavior,factFriend;
    var url =ANCHORPOST;
    var time = new Date().getTime();
  //  var page = {'pageNum':1,'pageSize':10};
    var param = {'uid':aid};
    $.ajax({
        url: url,
        type: 'POST',
        dataType:'json',
        data:JSON.stringify(param),
        beforeSend: function (xhr) {
            xhr.setRequestHeader("ts", time);
            xhr.setRequestHeader("mn", "3");
            xhr.setRequestHeader("chid", "0");
            xhr.setRequestHeader("version","1.0");
            xhr.setRequestHeader("sign", $.md5('ts='+ time +'&mn=3&chid=0&version=1.0&param='+JSON.stringify(param)))
        },
        async: false,
        cache: false,
        contentType: 'application/json;charset=UTF-8',
        success: function (data) {
            var datas = data.data;
            /*图片墙*/
            var htmlP = "";
            var tag='[';
            if(datas.photowall.indexOf(tag)!=-1)
            {
             var photoWall =JSON.parse(datas.photowall) ;
                 if(photoWall.length<=1){
                     var photosIMG = photoWall[0].data.url;
                     if(photoWall[0].type == 2){
                         photosIMG = photoWall[0].data.framePicUrl;
                         htmlP =' <li><img src="'+photosIMG+'" alt="" class="photoA"></li>';
                         $('#bannerPhoto').html(htmlP);
                     }else {
                         htmlP =' <li><img src="'+photosIMG+'" alt="" class="photoA"></li>';
                         $('#bannerPhoto').html(htmlP);
                     }
                 }else {
                     for(var i=0;i< photoWall.length;i++ ){
                         var photos = photoWall[i].data.url;
                         if(photoWall[i].type == 2){
                             photos = photoWall[i].data.framePicUrl;
                             htmlP +=' <li class="swiper-slide"><img src="'+photos+'" alt="" class="photoA"></li>';
                         }else {
                             htmlP +=' <li class="swiper-slide"><img src="'+photos+'" alt="" class="photoA"></li>';
                         }
                     }
                     $('#bannerPhoto').html(htmlP);
                 }


                } else {
                htmlP='<li><img src="'+datas.photowall+'" alt="" class="photoA"></li>';
                $('#bannerPhoto').html(htmlP);
            }
            name = datas.nickname;
            signText = datas.signature;
            age = datas.age+"岁";
            height = datas.height +"cm";
            weight = datas.weight + "kg";
            constellation = datas.constellation;
            linkType = datas.likeType;
            purpose = datas.purpose;
            headImg = datas.headimgurl;

            if(datas.acceptIntimateBehavior == 1){
                datas.acceptIntimateBehavior = "接受"
            }else {
                datas.acceptIntimateBehavior = "不接受"
            }
            if(datas.factFriend == 1){
                datas.factFriend = "接受"
            }else {
                datas.factFriend = "不接受"
            }
            factFriend = datas.factFriend;
            acceptIntimateBehavior = datas.acceptIntimateBehavior;
            $(".name").text(name);
            $(".age").text(age);
            $(".hiG").text(height);
            $(".weiG").text(weight);
            $(".constellation").text(constellation);
            $(".linkType").text(linkType);
            $(".purpose").text( purpose);
            $(".sign").text( signText);
            $(".acceptIntimateBehavior").text(acceptIntimateBehavior);
            $('.factFriend').text(factFriend);
        },
        error:function(res){
        }
    });
    /*判断是否被关注*/
    followed();
    function followed(){
        var token = localStorage.getItem('youyuLoginToken');
        var anchorUrl =FOLLOWED;
        var curTime = new Date().getTime();
        var packParam ={'aid':aid};
        var followState = {
            time: curTime,
            token:token,
            url:anchorUrl,
            param:packParam,
            sign:$.md5('ts='+curTime+'&mn=3&chid=0&version=1.0&token='+token+'&param='+JSON.stringify(packParam)),
            okCallback:function(data){
                if(data.data == true){
                    $(".followBtn").attr("src","../images/followChange.png");
                }else{
                    $(".followBtn").attr("src","../images/followlove.png");
                }



            },
            errorCallback:function(){
            }
        }
        YY.post(followState)
    }
    /*图片轮播*/
    var anchorSwiper = new Swiper('.swiper-container',{
        pagination: '.pagination',
        loop:true,
        grabCursor: true,
        paginationClickable: true,
        autoplay : 3000,
        autoplayDisableOnInteraction : false
    });
    /*点击关注*/
    $(".follow").click(function(){
        if($('.followBtn').attr('src') == '../images/followlove.png'){
            $(".followBtn").attr("src","../images/followChange.png");
            /*调用关注接口*/
            followA();
        }else {
            $(".followBtn").attr("src","../images/followlove.png");
            followCalc();
        }
    })

    /*关注主播*/

    function followA(){
        var token = localStorage.getItem('youyuLoginToken');
        var anchorUrl = FOLLOW_ANCHORPOST;
        var curTime = new Date().getTime();
        var packParam ={'aid':aid};
        var followPack = {
            time: curTime,
            token:token,
            url:anchorUrl,
            param:packParam,
            sign:$.md5('ts='+curTime+'&mn=3&chid=0&version=1.0&token='+token+'&param='+JSON.stringify(packParam)),
            okCallback:function(data){
            },
            errorCallback:function(){
            }
        }
        YY.post(followPack)
    }
    /*取消关注主播*/
    function followCalc(){
        var token = localStorage.getItem('youyuLoginToken');
        var anchorUrl = CANCEL_ANCHORPOST;
        var curTime = new Date().getTime();
        var packParam ={'aid':aid};
        var followCancel = {
            time: curTime,
            token:token,
            url:anchorUrl,
            param:packParam,
            sign:$.md5('ts='+curTime+'&mn=3&chid=0&version=1.0&token='+token+'&param='+JSON.stringify(packParam)),
            okCallback:function(data){
            },
            errorCallback:function(){
            }
        }
        YY.post(followCancel)
    }
})